package com.example.myapplication.User;

import static android.content.ContentValues.TAG;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Data.UserData;
import com.example.myapplication.R;
import com.example.myapplication.Tool.TimestampTool;

public class UserMessageActivity extends AppCompatActivity {
    private UserData userData;
    private TextView tvAccount;
    private ImageView ivAvatar;
    private TextView tvGender;
    private TextView tvSchool;
    private TextView tvCollage;
    private TextView tvUser;
    private TextView tvIdNum;
    private TextView tvIdNumText;
    private TextView tvPhone;
    private TextView tvEmail;
    private TextView tvRealname;
    private TextView tvSchooltime;
    private TextView tvSchooltimeText;
    private ImageView ivMessageExit;
    private Button btToedit;

    private int roleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_message);
        initView();
        getData();

        ivMessageExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 编辑资料
        btToedit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editUserData();
            }
        });
    }

    private void initView() {
        //获取数据
        userData = (UserData) getIntent().getSerializableExtra("userData");
        roleId = userData.getRoleId();
        tvAccount = findViewById(R.id.tv_account_text);
        ivAvatar = findViewById(R.id.iv_avatar);
        tvGender = findViewById(R.id.tv_gender_text);
        tvSchool = findViewById(R.id.tv_school_text);
        tvCollage = findViewById(R.id.tv_collage_text);
        tvUser = findViewById(R.id.tv_id_text);
        tvIdNum = findViewById(R.id.tv_id_num);
        tvIdNumText = findViewById(R.id.tv_id_num_text);
        tvPhone = findViewById(R.id.tv_phone_text);
        tvEmail = findViewById(R.id.tv_email_text);

        tvRealname = findViewById(R.id.tv_realname_text);
        tvSchooltimeText = findViewById(R.id.tv_schooltime_text);
        tvSchooltime = findViewById(R.id.tv_schooltime);
        btToedit= findViewById(R.id.bt_toedit);
        ivMessageExit = findViewById(R.id.iv_message_exit);
        Log.d("cpy---userdata", String.valueOf(userData));
    }
    private void getData(){
        tvAccount.setText(userData.getUserName());
        if (ivAvatar == null) {
            Log.e(TAG, "ImageView iv_avatar is null");
            return;
        }

        if(userData.getAvatar()!=null){
            Glide.with(this)
                    .load(userData.getAvatar())//图片网址
                    .circleCrop()
                    .into(ivAvatar);
        }
        if(userData.getGender()==true){
            tvGender.setText("男");
        }
        else {
            tvGender.setText("女");
        }

        tvSchool.setText("桂林电子科技大学");
        tvCollage.setText(userData.getCollegeName());
        if(userData.getRoleId()==0){
            tvIdNum.setText("学号");
            tvSchooltime.setText("入学时间");
            tvUser.setText("学生");
        }else {
            tvIdNum.setText("工号");
            tvSchooltime.setText("入职时间");
            tvUser.setText("老师");
        }
        tvIdNumText.setText(String.valueOf(userData.getIdNumber()));
        tvPhone.setText(userData.getPhone());
        tvEmail.setText(userData.getEmail());
        tvRealname.setText(userData.getRealName());
        String schoolTime = Long.toString(userData.getInSchoolTime());
        tvSchooltimeText.setText(TimestampTool.timestampToDateTime(schoolTime));
    }

    //编辑资料
    private void editUserData() {
        Intent editIntent = new Intent(UserMessageActivity.this, EditUserMessageActivity.class);
        editIntent.putExtra("userData", userData);
        startActivity(editIntent);
    }

}